/**
 * 
 */
package sailing.ui.model;

import java.util.List;

import org.jdesktop.application.SingleFrameApplication;

import sailing.entity.Customer;
import sailing.managers.CustomerManager;
import sailing.utils.Utils;
import cjlite.utils.Lists;

/**
 * @author kevin
 * 
 */
public class CustomerTableModel extends SailingTableModel {

	String[] columnName = { "客户名称", "创建时间", "编辑操作" };
	private boolean showEditBtn = false;

	private List<Customer> customerList = Lists.newArrayList();


	/**
	 * @param application
	 */
	public CustomerTableModel(SingleFrameApplication application) {
		super(application);
	}


	@Override
	public void fireTableDataChanged() {
		this.updateModel();
		super.fireTableDataChanged();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return customerList.size();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return showEditBtn ? columnName.length : columnName.length - 1;
	}


	@Override
	public String getColumnName(int column) {
		return columnName[column];
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (rowIndex >= this.customerList.size()) {
			return "";
		}

		Customer c = this.customerList.get(rowIndex);

		switch (columnIndex) {
		case 0:
			return c.getName();
		case 1:
			return Utils.dateFormat(c.getCreateTime());
		case 2:
			return c;
		}

		return "";
	}


	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return showEditBtn && columnIndex == 2;
	}


	@Override
	public void updateModel() {
		CustomerManager cm = this.getApplication().getContext().getIoc().getInstance(CustomerManager.class);
		customerList.clear();
		customerList.addAll(cm.getAllCustomerList());
	}


	public void showEditBtn(boolean selected) {
		showEditBtn = selected;
	}

}
